using Microsoft.AspNetCore.SignalR;

namespace ProduceBasicData.Api.Hubs
{
    /// <summary>
    /// 通知中心
    /// </summary>
    public class NotificationHub : Hub
    {
        /// <summary>
        /// 当客户端连接到Hub时调用
        /// </summary>
        /// <returns></returns>
        public override async Task OnConnectedAsync()
        {
            await base.OnConnectedAsync();
        }

        /// <summary>
        /// 当客户端从Hub断开连接时调用
        /// </summary>
        /// <param name="exception"></param>
        /// <returns></returns>
        public override async Task OnDisconnectedAsync(Exception? exception)
        {
            await base.OnDisconnectedAsync(exception);
        }

        /// <summary>
        /// 发送通知给所有连接的客户端
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task SendNotificationToAll(string message)
        {
            await Clients.All.SendAsync("ReceiveNotification", message);
        }

        /// <summary>
        /// 发送通知给特定用户
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task SendNotificationToUser(string userId, string message)
        {
            await Clients.User(userId).SendAsync("ReceiveNotification", message);
        }
    }
}




